package com.qnzw.zjmy.ad.kaku;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATNetworkConfirmInfo;
import com.anythink.core.api.AdError;
import com.anythink.splashad.api.ATSplashAd;
import com.anythink.splashad.api.ATSplashAdExtraInfo;
import com.anythink.splashad.api.ATSplashExListener;
import com.qnzw.zjmy.ad.impl.AdRevenueListenerImpl;
import com.qnzw.zjmy.ad.inter.RewardAdSplasInter;
import com.qnzw.zjmy.home.ui.enums.TaKuAdPositionEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 开屏广告
 */
public class TaKuSplashAd implements ATSplashExListener {

    private ATSplashAd splashAd;

    private Activity mActivity;
    private RewardAdSplasInter mRewardAdSplasInter;
    private String token;
    private ViewGroup viewGroup;

    private TaKuSplashAd() {
    }

    private static volatile TaKuSplashAd instance;

    public static synchronized TaKuSplashAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (TaKuSplashAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new TaKuSplashAd();
                }
            }
        }
        return instance;
    }

    public void load(Context context, String placementId, RewardAdSplasInter rewardAdSplasInter, ViewGroup container) {
        this.mRewardAdSplasInter = rewardAdSplasInter;
        this.viewGroup = container;
        splashAd = new ATSplashAd(context, placementId, this, 5000);
        splashAd.setAdRevenueListener(new AdRevenueListenerImpl(context, token, TaKuAdPositionEnum.AD_SPLASH_POSITION.getKey(), "0"));
        splashAd.loadAd();
    }

    @Override
    public void onDeeplinkCallback(ATAdInfo atAdInfo, boolean b) {
    }

    @Override
    public void onDownloadConfirm(Context context, ATAdInfo atAdInfo, ATNetworkConfirmInfo atNetworkConfirmInfo) {
    }

    @Override
    public void onAdLoaded(boolean b) {
        if (!b && splashAd.isAdReady()) {
            //container大小至少占屏幕75%
            splashAd.show(mActivity, viewGroup);
            mRewardAdSplasInter.closeAlert();
        }

    }

    @Override
    public void onAdLoadTimeout() {
        mRewardAdSplasInter.onError();
        Log.e("yqq", "广告超时了");
    }

    @Override
    public void onNoAdError(AdError adError) {
        mRewardAdSplasInter.onError();
        Log.e("yqq", adError.getDesc());
    }

    @Override
    public void onAdShow(ATAdInfo atAdInfo) {
//        Log.e("yqq", "广告展示了");
    }

    @Override
    public void onAdClick(ATAdInfo atAdInfo) {
//        Log.e("yqq", "点击广告了");
    }

    @Override
    public void onAdDismiss(ATAdInfo atAdInfo, ATSplashAdExtraInfo atSplashAdExtraInfo) {
        mRewardAdSplasInter.onAdClose();
        Log.e("yqq", "广告关闭了");
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setmActivity(Activity mActivity) {
        this.mActivity = mActivity;
    }
}
